package com.example.node.controller;

import com.example.node.factory.NodeFactory;
import com.example.node.message.Request;
import com.example.node.message.Response;
import com.example.node.utils.IdUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author chentao
 * @version 1.0
 * @description: TODO
 * @date 2025/10/25 22:09
 */
@RestController
public class NodeController {

    @Resource
    private NodeFactory nodeFactory;

    /***
     * 查询活跃节点数量，默认不保函本节点
     *
     * @return
     */
    @RequestMapping(value = "getActiveSize")
    public int getActiveSize() {
        return this.nodeFactory.getNode().getActiveSize();
    }

    /***
     * 查询活跃节点的key列表，默认不保函本节点
     *
     * @return
     */
    @RequestMapping(value = "getActiveKeys")
    public List<String> getActiveKeys() {
        return this.nodeFactory.getNode().getActiveKeys();
    }

    /***
     * 发送请求，默认不保函本节点
     *
     * @param receiverId
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "send")
    public Response send(Long receiverId) throws Exception {
        Request request = new Request(receiverId, IdUtils.getId());
        return this.nodeFactory.getNode().send(request);
    }

    /***
     * 发送请求，默认不保函本节点, 集群广播模式
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "sendAll")
    public List<Response> sendAll() throws Exception {
        Request request = new Request(IdUtils.getId());
        return this.nodeFactory.getNode().sendAll(request);
    }
}
